using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Colorprj.Masters;
namespace Colorprj
{
    public partial class frmParty : Form
    {
        PartyManager pm = new PartyManager();
        public frmParty()
        {
            InitializeComponent();
        }
        private void btnSave_Click(object sender, EventArgs e) {
            try {
            Party pEntity = getFormData();
            pm.AddParty(pEntity); 
            setAddMode();
            ClearForm(this.Controls);
            MessageBox.Show("Record Saved !");
              }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
        }
        private void ClearForm(Control.ControlCollection coll)
        {
           // Control.ControlCollection coll = this.Controls;
            foreach (Control c in coll) {
                if (c.HasChildren) ClearForm(c.Controls);
                if (c is TextBox) c.Text = string.Empty;
                //if (c is DataGridView) { ((DataGridView)c).Rows.Clear(); }
                if (c is ComboBox) {
                    ((ComboBox)c).SelectedIndex = -1;
                }
            }
        }
        private Party getFormData() {
            Party pEntity = new Party();
            //try {
            
            if (!string.IsNullOrEmpty(txtId.Text))
            pEntity.PartyId = Convert.ToInt16(txtId.Text);
            pEntity.PartyName = txtName.Text ;
            pEntity.PartyAddress1  = txtAddress1.Text  ;
            pEntity.PartyAddress2  = txtAddress2.Text  ;
            pEntity.PartyCity = txtCity.Text ;
            pEntity.PhoneNo  = txtPhone.Text ;
            pEntity.EmailId  = txtEmail.Text ;
        //}
        //catch (Exception ex)
        //{
        //    MessageBox.Show(ex.Message);
        //}
            return pEntity;

        }
        private void setAddMode()
        {
            btnSave.Enabled = true; 
            btnDelete.Enabled = false;
            btnUpdate.Enabled = false;
            dataGridView1.DataSource = pm.GetAllParties().Tables[0];

        }
        private void setEditDelMode()
        {

            btnDelete.Enabled = true;
            btnUpdate.Enabled = true;
            btnSave.Enabled = false;


        }
        private void frmParty_Load(object sender, EventArgs e)
        {
            setAddMode();
        }
        private void dataGridView1_RowHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            setEditDelMode();
            txtAddress1.Text = dataGridView1["PartyAddress1", e.RowIndex].Value.ToString();
            txtAddress2.Text = dataGridView1["PartyAddress2", e.RowIndex].Value.ToString();
            txtName.Text = dataGridView1["PartyName", e.RowIndex].Value.ToString();
            txtId.Text = dataGridView1["Partyid", e.RowIndex].Value.ToString();
            txtCity.Text = dataGridView1["PartyCity", e.RowIndex].Value.ToString();
            txtPhone.Text = dataGridView1["PhoneNo", e.RowIndex].Value.ToString();
            txtEmail.Text = dataGridView1["EmailId", e.RowIndex].Value.ToString();

        }
        private void btnUpdate_Click(object sender, EventArgs e)
        {
            try {
                Party pEntity = getFormData();
                pm.UpdParty(pEntity);
                setAddMode();
                ClearForm(this.Controls);
                MessageBox.Show("Record Saved !");
            } catch (Exception ex) {
                MessageBox.Show(ex.Message);
            }
        }
        private void btnCancel_Click(object sender, EventArgs e)
        {
            setAddMode();
            ClearForm(this.Controls);
        }
        private void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                DialogResult result =  MessageBox.Show("Want To delete this record ? ", "", MessageBoxButtons.YesNo);

                if (result == System.Windows.Forms.DialogResult.Yes)
                {
                    Party pEntity = getFormData();
                    pm.DelParty(pEntity.PartyId);
                    setAddMode();
                    ClearForm(this.Controls);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

    }
}